package com.example.sqlitetest.Activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.sqlitetest.R;
import com.example.sqlitetest.db.MyDataBaseHelper;
import com.example.sqlitetest.model.LiuYanNumInfo;

public class LiuYanSearchActivity  extends AppCompatActivity {
    EditText addText1;
    private MyDataBaseHelper helper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*导入界面*/
        setContentView(R.layout.activity_liuyan_search);

        helper = new MyDataBaseHelper(this);//创建并初始化DBOpenHelper对象
        Button search = findViewById(R.id.search);

        //导入文本框
        addText1 = findViewById(R.id.addText1);

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username  = addText1.getText().toString();
//                Bundle b = new Bundle();
////                b.putString("username",username);
////                Toast.makeText(LiuYanSearchActivity.this,"开始查询", Toast.LENGTH_SHORT).show();
////                Intent intent = new Intent(LiuYanSearchActivity.this,LiuYanNumListActivity.class);
////                intent.putExtras(b);
////                startActivity(intent);

                SQLiteDatabase sqLiteDatabase = helper.getWritableDatabase();
                Cursor cursor = sqLiteDatabase.query("liuyan", null, "username="+username, null, null, null, null);
                if (cursor.moveToFirst()) {
                    int num=0;
                    do {
                        num++;
                    } while (cursor.moveToNext());
                    Toast.makeText(LiuYanSearchActivity.this,"留言次数为:"+num, Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(LiuYanSearchActivity.this,"该学生没有留言过", Toast.LENGTH_SHORT).show();
                }
                cursor.close();
            }
        });
}
}
